var util = require('../../utils/util');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
import { $wuxLoading } from '../../dist/index'

var app = getApp();
var that;
var pageInex = 1;

Page({
  data: {
    money: 0,
    user:null,
	empty_msg:app.globalData.empty_msg,
    list : [],
    isLoadMore : false,
    isEnd : false,
    isEmpty:false,
    inputShowed: false,
    inputVal: "",
	flag:true,
	tkamount:""
	,haverefund:false
	,refund:null
  },
  onPullDownRefresh:function(){
    pageInex = 1;
    this.start=true;
    that.setData({
      list : [],
      isLoadMore : false,
      isEnd : false,
      isEmpty:true
    });
    util.showLoading();
    that.loadData();
  },
  onReachBottom : function(e,a){
      if(!that.data.isEnd && !that.data.isLoadMore && !that.data.isEmpty){
          that.loadData();
      }
  },

  onLoad:function(){
	  util.loading = $wuxLoading();
    pageInex = 1;
    that = this;
    util.showLoading();
	
    app.getUserInfo()
    .then(res => {
		return wxRequest.postRequest(config.myrefund, {});
    })
    .then(res => {
        if(util.judge.isObject(res.item)){
			that.setData({
			  haverefund: true,
			  refund:res.item
			});
		}
		return wxRequest.postRequest(config.mybalance, {});
    })
	.then(res => {
		
		that.setData({
          money: (res.user.amount / 100)
        });
		
    })
    .catch(res => {
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });
	
	that.loadData();
	

  },
  refund:function(){
	
		that.setData({
          flag: false
        });
	  
  }
  
  ,loadData:function(){
	util.showLoading();
	app.getUserInfo()
    .then(res => {
        var params = {
          pageIndex: pageInex,
          pageSize: app.globalData.defaultPageSize
        }
        return wxRequest.postRequest(config.daybook, params)
    })
    .then(res => {
        res = res.list;

        if(pageInex==1 && res.length==0){
            that.setData({
              isLoadMore : false,
              isEnd : false,
              isEmpty :true,
            });
            return;
        }

        if(res.length > 0){
          pageInex++;

          for(var i in res){
           
          }
          var data = that.data.list.concat(res);   
          that.setData({
              list : data,
              isLoadMore : false
			  ,isEmpty :false
          });
        }
        
        if(res.length < app.globalData.defaultPageSize || res.length == 0 ){
          that.setData({
            isLoadMore : false,
            isEnd : true
			,isEmpty :false
          });
        }

    })
    .catch(res => {
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });
  }
  ,tkcancel:function(){
	  that.setData({
        flag: true
    });

  }
  
  ,qbtk:function(){
	  that.setData({
      tkamount: that.data.money
    });

  }
  ,bindKeyInput: function(e) {
    this.setData({
      tkamount: e.detail.value
    })
  }
  ,tksave:function(){
	
	  var params = {};
    params.amount = that.data.tkamount;
	
    if(params.amount > 200 || params.amount < 1){
      util.alert("退款金额 1-200 元");
      return false;
    }
 
    wxRequest.postRequest(config.refund, params)
    .then(res => {
      if(res==1){
        util.hideLoading();
        util.alert("申请退款成功,",function(){
          that.setData({
              flag: true,
              tkamount: "",
              list : [],
              isLoadMore : false,
              isEnd : false,
              isEmpty:false
          });
          that.onLoad();
        });
      }
    })
    .catch(res => {
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });


  }
  
  
  
})
